package me.heyclock.hl.recordwriter.tools;

import android.content.Context;
import android.graphics.Typeface;
import android.support.constraint.ConstraintLayout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Author:hl
 * Time:  2018/5/24 11:34
 * Des:   This is HC_Screen
 */
public class HC_Screen
{
	///< 屏幕宽高【三维】
	public static int SCREEN_HEIGHT = 1920;
	public static int SCREEN_WIDTH = 1080;
	///< 实际屏幕宽高【像素】
	public static int REAL_SCREEN_HEIGHT = 1920;
	public static int REAL_SCREEN_WIDTH = 1080;
	public static float ratio_width;
	public static float ratio_height;
	//public static Typeface textTypeface;
	///< 上下文
	private Context context;
	///< 实例
	private static HC_Screen uniqueInstance = null;

	///< 状态栏高度
	public static int STATUS_BAR_HEIGHT = 0;
	
	private HC_Screen() {
	}

	/**
	 * 初始化并设置上下文
	 */
	public static void init(Context mContext)
	{
		///< 字体
		//textTypeface = Typeface.createFromAsset (context.getAssets() , "huakanghaibaotiW12.ttc" );
		DisplayMetrics dm = new DisplayMetrics();
		dm = mContext.getResources().getDisplayMetrics();
		//activity.getWindowManager().getDefaultDisplay().getMetrics(dm);		///<  获取屏幕分辨率
		SCREEN_HEIGHT = (int) (REAL_SCREEN_HEIGHT = SCREEN_HEIGHT = dm.heightPixels);
		SCREEN_WIDTH  = (int) (REAL_SCREEN_WIDTH = SCREEN_WIDTH = dm.widthPixels);
		///< 如果宽大于高，宽高互换
		if(SCREEN_WIDTH > SCREEN_HEIGHT)
		{
			int temp = SCREEN_HEIGHT;
			SCREEN_HEIGHT = REAL_SCREEN_HEIGHT = SCREEN_WIDTH;
			SCREEN_WIDTH = REAL_SCREEN_WIDTH = temp;
		}

		ratio_height = ratio_width = (float) ((Math.sqrt(SCREEN_HEIGHT*SCREEN_HEIGHT+SCREEN_WIDTH*SCREEN_WIDTH))/(Math.sqrt(1080*1080+1920*1920)));
	}
	
	public void onDestroy()
	{
		uniqueInstance = null;
	}
	
	/** 
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
	 */  
	public static int dip2px(Context context, float dpValue) 
	{  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (dpValue * scale + 0.5f);  
	}  

	/** 
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
	 */  
	public static int px2dip(Context context, float pxValue)
	{  
		final float scale = context.getResources().getDisplayMetrics().density;  
		return (int) (pxValue / scale + 0.5f);  
	} 
	
	/**
	 * 设置边距 - 参数如果为-10000， 则表示使用原始值
	 */
	public static void setMargin(View v, int _l, int _t, int _r, int _b) 
	{  
	    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) 
	    {  
	    	ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();  
	        int l = (int) (_l*ratio_width), t = (int) (_t*ratio_height), 
	        	r = (int) (_r*ratio_width), b = (int) (_b*ratio_height);
	        if (-10000 == _l)
	        {
	        	l = p.leftMargin;
	        }
	        if (-10000 == _r)
	        {
	        	r = p.rightMargin;
	        }
	        if (-10000 == _t)
	        {
	        	t = p.topMargin;
	        }
	        if (-10000 == _b)
	        {
	        	b = p.bottomMargin;
	        }
	        p.setMargins(l, t, r, b);  
	        v.requestLayout(); 
	    }  
	}
	
	/**
	 * 设置边距 - 参数如果为-10000， 则表示使用原始值
	 */
	public static void setMarginByW(View v, int _l, int _t, int _r, int _b) 
	{  
	    if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) 
	    {  
	    	ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams();  
	        int l = (int) (_l*ratio_width), t = (int) (_t*ratio_width), 
	        	r = (int) (_r*ratio_width), b = (int) (_b*ratio_width);
	        if (-10000 == _l)
	        {
	        	l = p.leftMargin;
	        }
	        if (-10000 == _r)
	        {
	        	r = p.rightMargin;
	        }
	        if (-10000 == _t)
	        {
	        	t = p.topMargin;
	        }
	        if (-10000 == _b)
	        {
	        	b = p.bottomMargin;
	        }
	        p.setMargins(l, t, r, b);  
	        v.requestLayout(); 
	    }  
	}
	
	///< 设置组件的padding值
	public static void setPadding(View v,int left,int top,int right,int bottom)
	{
		v.setPadding((int)(left*ratio_width), (int)(top*ratio_height), (int)(right*ratio_width), (int)(bottom*ratio_height));
	}
	
	///< 设置父组件是ViewGroup的控件的宽高
	public static void setViewGroupWH(View v, int w, int h) 
	{ 
		ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
			layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是ViewGroup的控件的宽高(高和宽相同)
	public static void setViewGroupByW(View v, int w) 
	{ 
		ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (w*ratio_width);
		layoutT.height = (int) (w*ratio_width);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是ViewGroup的控件的宽高（宽和高相同）
	public static void setViewGroupByH(View v, int h) 
	{ 
		ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (h*ratio_height);
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是ViewGroup的控件的宽
	public static void setViewGroupW(View v, int w) 
	{ 
		ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是ViewGroup的控件的高
	public static void setViewGroupH(View v,int h) 
	{ 
		ViewGroup.LayoutParams layoutT = (ViewGroup.LayoutParams) v.getLayoutParams();
			layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是LinearLayout的控件的宽高
	public static void setLinearLayoutWH(View v, int w, int h) 
	{ 
		LayoutParams layoutT = (LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
			layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是LinearLayout的控件的宽
	public static void setLinearLayoutW(View v, int w) 
	{ 
		LayoutParams layoutT = (LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是LinearLayout的控件的宽
	public static void setLinearLayoutByW(View v, int w) 
	{ 
		LayoutParams layoutT = (LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (w*ratio_width);
		layoutT.height = layoutT.width;
		v.setLayoutParams(layoutT);
	}
		
	///< 设置父组件是LinearLayout的控件的宽
	public static void setLinearLayoutByH(View v, int h) 
	{ 
		LayoutParams layoutT = (LayoutParams) v.getLayoutParams();
		layoutT.height = (int) (h*ratio_height);
		layoutT.width = layoutT.height;
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是LinearLayout的控件的高
	public static void setLinearLayoutH(View v,int h) 
	{ 
		LayoutParams layoutT = (LayoutParams) v.getLayoutParams();
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	/// < 设置父组件是RelativeLayout的控件的宽高
	public static void setFrameLayoutWH(View v, int w, int h) {
		FrameLayout.LayoutParams layoutT = (FrameLayout.LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (w * ratio_width);
		layoutT.height = (int) (h * ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是RelativeLayout的控件的宽高
	public static void setRelativeLayoutWH(View v, int w, int h) 
	{ 
		RelativeLayout.LayoutParams layoutT = (RelativeLayout.LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
			layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}

	///< 设置父组件是ConstraintLayout的控件的宽高
	public static void setConstraintLayoutWH(View v, int w, int h)
	{
		ConstraintLayout.LayoutParams layoutT = (ConstraintLayout.LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (w*ratio_width);
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是RelativeLayout的控件的宽（高不变）
	public static void setRelativeLayoutW(View v, int w) 
	{ 
		RelativeLayout.LayoutParams layoutT = (RelativeLayout.LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是RelativeLayout的控件的高(宽不变)
	public static void setRelativeLayoutH(View v,int h) 
	{ 
		RelativeLayout.LayoutParams layoutT = (RelativeLayout.LayoutParams) v.getLayoutParams();
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是RelativeLayout的控件的宽高（高和宽相同）
	public static void setRelativeLayoutByW(View v, int w) 
	{ 
		RelativeLayout.LayoutParams layoutT = (RelativeLayout.LayoutParams) v.getLayoutParams();
			layoutT.width = (int) (w*ratio_width);
			layoutT.height = (int) (w*ratio_width);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父组件是RelativeLayout的控件的宽高（高和宽相同）
	public static void setRelativeLayoutByH(View v, int h) 
	{ 
		RelativeLayout.LayoutParams layoutT = (RelativeLayout.LayoutParams) v.getLayoutParams();
		layoutT.height = (int) (h*ratio_height);
		layoutT.width = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}
	
	///< 设置父窗口为AbsListView的控件宽高
	public static void setAbsListViewWH(View v, int w, int h) 
	{ 
		AbsListView.LayoutParams layoutT = (AbsListView.LayoutParams) v.getLayoutParams();
		layoutT.width = (int) (w*ratio_width);
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}

	///< 设置父窗口为AbsListView的控件宽高
	public static void setAbsListViewH(View v, int h) 
	{ 
		AbsListView.LayoutParams layoutT = (AbsListView.LayoutParams) v.getLayoutParams();
		layoutT.height = (int) (h*ratio_height);
		v.setLayoutParams(layoutT);
	}

	/**
	 * 设置控件字体大小
	 * @param view
	 * @param textSize
	 */
	public static  void setTextSize(View view,int textSize)
	{
		float size = textSize * ratio_height < 19 ? 19 : textSize * ratio_height;
		if(view instanceof Button)
		{
			((Button)view).setTextSize(TypedValue.COMPLEX_UNIT_PX,  size);
		}
		else if(view instanceof TextView)
		{
			((TextView)view).setTextSize(TypedValue.COMPLEX_UNIT_PX,  size);
		}
		else if(view instanceof EditText)
		{
			((EditText)view).setTextSize(TypedValue.COMPLEX_UNIT_PX,  size);
		}
	}
	
	/**
	 * 输入法显示或隐藏
	 * @param _context
	 * @param bShow
	 */
	public static void showKeyBorder(Context _context, View v, boolean bShow)
	{
		///< 隐藏输入法
		InputMethodManager imm = (InputMethodManager) _context.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
		if (bShow)
		{
			
		}
		else
		{
			imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
		}
	}
}
